import api from './api'; 
const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

const getUrlParams = function( url ){

    var params = {};
    var urls = url.split("?");           
    var arr = urls[1].split("&");
    for (var i = 0, l = arr.length; i < l; i++) {
      var a = arr[i].split("=");  
      params[a[0]] = a[1]; 
    }
    return params;
  
}


const chooseImage = function ( cb , cb2 ){
  //选择并上传图片
  var me = this;

  wx.chooseMedia({
      count: 9,
      mediaType: ['image','video'],
      sourceType: ['album', 'camera'],
      maxDuration: 30,
      camera: 'back',
      success(res) {
          console.log(res);
        //console.log(res.tempFiles.tempFilePath)
        //console.log(res.tempFiles.size)

        if(res.errMsg=="chooseMedia:ok"){
              var files = res.tempFiles;
              //var tempFilePaths = res.tempFilePaths;
              //me.uploadImage(files[0].tempFilePath)


              cb( files[0].tempFilePath )
        }
          /*
          wx.uploadFile({ 
              url: '/your_url/receive_file', // 图片上上传的地址，请求方式默认为POST且不可更改
              filePath: tempFilePaths[0], // 要上传的文件的路径，注：一次只能上传一个文件，若要上传多张图片，请使用递归
              name: 'file', // 文件对应的键名，后端可以通过这个key获取到文件的二进制内容
              formData:{
                  'user_id': '123',
                  'name': 'Jack',
                  'age': 18
              },
              success: function(res){
                  var data = res.data
                  //do something

                  console.log(data);
              }
          })

          */


      }
    })

}
const uploadImage = function( temp_file ,cb ){
  console.log("upload_comment_image")
  var me = this;
  api.user.uploadCommentImage( temp_file , {} , function( response ){
      console.log(response);
      var res = JSON.parse(response);


      cb(response.data);
      /*
      if(res.status==1){
          var image_list = me.data.image_list;
          image_list.push( res.data.url)

          console.log(image_list);
          me.setData({
              image_list: image_list
          })
      }

      */
  })

  /*
  http.uploadFile("/common/upload" , temp_file , {} , function ( response ){
      console.log(response);
      var res = JSON.parse(response);
      if(res.status==1){
          var image_list = me.data.image_list;
          image_list.push( res.data.url)

          console.log(image_list);
          me.setData({
              image_list: image_list
          })
      }
  })

  */
}

module.exports = {
  formatTime: formatTime,
  getUrlParams: getUrlParams,
  chooseImage: chooseImage,
  uploadImage: uploadImage

}
